package com.manelrodriguez.applications.ibex35.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.manelrodriguez.applications.ibex35.R;
import com.manelrodriguez.applications.ibex35.adapters.CarteraMovListAdapter;
import com.manelrodriguez.applications.ibex35.bo.MovimientoBO;
import com.manelrodriguez.applications.ibex35.to.Movimiento;
import com.manelrodriguez.applications.ibex35.utils.Constantes;

public class CarteraVerMovimientosActivity extends Activity implements OnItemClickListener{
	private Intent intent;
	private static List<Movimiento> movimientos;
	private CarteraMovListAdapter cartera_adapter;
	private MovimientoBO movBO;
	//private boolean detalleEmpresa = false;
	String idEmpresa = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cartera_mov);
		movBO = new MovimientoBO(this);
		Bundle extras = getIntent().getExtras();
		if (extras != null){
			Intent i = getIntent();
			idEmpresa = (String) i.getStringExtra(Constantes.EMPRESA);
			if("".equals(idEmpresa)){
				//detalleEmpresa = false;
				movimientos = movBO.getMovimientos();
			}else{
				//detalleEmpresa= true;
				movimientos = movBO.getMovimientosEmpresa(idEmpresa);
			}
		}
		
		if(movimientos!=null && movimientos.size()>0){
			this.cartera_adapter = new CarteraMovListAdapter(this, R.layout.cartera_item_mov, movimientos);
			ListView lv2 = (ListView)findViewById(R.id.listView2_mov);
			lv2.setAdapter(this.cartera_adapter);
	        lv2.setOnItemClickListener(this);
	        this.registerForContextMenu(lv2);
		}
	}
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		Movimiento mov = movimientos.get(info.position);
	    menu.setHeaderTitle(mov.getIdEmpresa());
	    menu.add(0, v.getId(), 0, getString(R.string.editar));
	    menu.add(0, v.getId(), 0, getString(R.string.borrar));
    }
	
	@Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	if (item.getTitle().equals(getString(R.string.editar))) {
    		intent = new Intent(getApplicationContext(), CarteraAddIemActivity.class);
    		intent.putExtra(Constantes.MOVIMIENTO,movimientos.get(info.position).getIdMovimiento());
    		startActivityForResult(intent,2);
    	}else if (item.getTitle().equals(getString(R.string.borrar))){
    		movBO.borrarMovimiento(movimientos.get(info.position).getIdMovimiento());
    		cartera_adapter.remove(cartera_adapter.getItem(info.position));
    		Toast.makeText(CarteraVerMovimientosActivity.this, getString(R.string.borrado), Toast.LENGTH_SHORT).show();
    		cartera_adapter.notifyDataSetChanged();
    	}

       return true;
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK && requestCode==2){
			/*if(detalleEmpresa){
				movimientos = movBO.getMovimientosEmpresa(idEmpresa);
			}else{
				movimientos = movBO.getMovimientos();
			}*/
			Movimiento mov = (Movimiento)data.getExtras().getParcelable("MOVIMIENTO");
			
			for(int i = 0; i<this.cartera_adapter.getCount();i++){
				Movimiento aux = this.cartera_adapter.getItem(i);
				if(aux.getIdMovimiento() == mov.getIdMovimiento()){
					cartera_adapter.remove(aux);
					cartera_adapter.insert(mov, i);
				}
			}
			Toast.makeText(this, getString(R.string.anadido), Toast.LENGTH_SHORT).show();
			cartera_adapter.notifyDataSetChanged();
		}
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {}
}
